#include <stdio.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h> /* superset of previous */
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include "cJSON.h"
#define N 64

int main(int argc, char const *argv[])
{
    int broadfd;

    // 创建一个socket文件描述符
    broadfd = socket(AF_INET, SOCK_DGRAM, 0);
    if (broadfd < 0)
    {
        perror("sock err");
        return -1;
    }

    // 绑定套接字(ip+port)
    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(8888); // 端口号
    addr.sin_addr.s_addr = INADDR_ANY;

    int addrlen = sizeof(addr);

    if (bind(broadfd, (struct sockaddr *)&addr, addrlen) < 0)
    {
        perror("bind err");
        return -1;
    }

    ssize_t len;
    char buf[N] = {0};
    struct sockaddr_in cliaddr;

    // 接收"coffee"搜索包
    bzero(buf, N);
    len = recvfrom(broadfd, buf, N, 0, (struct sockaddr *)&cliaddr, &addrlen);

    // 判断是否是本公司产品：收到的数据是否"coffee"
    if (strcmp(buf, "coffee") != 0)
    {
        printf("%s\n", buf);
        printf("not my coffee\n");
        return -1;
    }
    else
        // 回复yes，告诉软件，我收到了搜索协议，并且回复地址
        sendto(broadfd, "yes", 4, 0, (struct sockaddr *)&cliaddr, addrlen);

    // 变身为TCP服务器，准备接收软件的升级文件
    int tcpfd = socket(AF_INET, SOCK_STREAM, 0);
    if (tcpfd < 0)
    {
        perror("sock err");
        return -1;
    }

    if (bind(tcpfd, (struct sockaddr *)&addr, addrlen) < 0)
    {
        perror("bind err");
        return -1;
    }

    // 监听套接字
    if (listen(tcpfd, 5) < 0)
    {
        perror("listen err");
        return -1;
    }

    // 接收客户端的连接
    printf("wait client connect\n");
    int clifd;

    // 接收对端的地址
    clifd = accept(tcpfd, NULL, NULL);
    if (clifd < 0)
    {
        perror("accept err");
        return -1;
    }
    printf("new cononect coming\n");
    int client_sock;
    char buffer[1024] = "0";
    int received_bytes = recv(clifd, buffer, 1024, 0);
    if (received_bytes >= 0)
    {
        buffer[received_bytes] = '\0';
        printf("Received JSON: %s\n", buffer);
    }
    else
    {
        printf("err 1111\n");
    }
    cJSON *root = cJSON_Parse(buffer);
    cJSON *item = cJSON_GetObjectItem(root, "type");
    printf("%s:%d\n", item->string, item->valueint);

    cJSON *temp = cJSON_GetObjectItem(root, "data");
    if (temp == NULL)
    {
        cJSON_AddItemToObject(root, "result", cJSON_CreateNumber(0));
        cJSON_AddItemToObject(root, "up_config", cJSON_CreateBool(0));
        char *json_str = cJSON_PrintUnformatted(root);
        // printf("测试数据：%ld\n",sizeof(*json_str));
        int received_bytes2 = send(clifd, json_str, strlen(json_str), 0);
        if (received_bytes > 0)
        {
            printf("成功发送！\n");
        }
        else
        {
            printf("发送失败！ #2\n");
        }
        int received_bytes3 = recv(clifd, buffer, sizeof(buffer), 0);
        if (received_bytes3 > 0)
        {
            buffer[received_bytes3] = '\0';
            printf("Received JSON_Data: %s\n", buffer);
        }
        else
        {
            printf("接受失败！ #3\n");
        }
    }
    else
    {
        printf("已存在\n");
    }

    close(clifd);
    close(tcpfd);
    close(broadfd);
    return 0;
}